﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Utilities;
using SpringWaterDataAccess;
using System.Data.SqlClient;
using System.Text;

namespace test
{
	/// <summary>
	/// Summary description for forumPostDetail.
	/// </summary>
	public partial class forumPostDetail : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			string subject;
			string body;
			string user;
			string date;
			string topicID;

			string PostID = Request.QueryString["PostID"];

			// Put user code to initialize the page here
			if (!IsPostBack) 
			{
		
				ForumDataAccess da = new ForumDataAccess();
				da.IncreaseClicks(Convert.ToInt32(PostID));
				da.GetPostDetail(PostID, out subject, out body, out user, out date, out topicID);
				lblSubject.Text = subject;
				//lblBody.Text = "<pre>" +body + "</pre>";
				//TEXTAREA1.Value = body;
				//TEXTAREA1.Rows = body==null ? 0 : Math.Min(body.Length/30,20);
				Hashtable bodyHash = new Hashtable();
				bodyHash.Add("body",body.Replace("\n","<br>"));

				DataList1.DataSource = bodyHash;
				DataList1.DataBind();
			
		
				lblAuthor.Text = user;
				lblDate.Text = Helper.ConvertDate(date, Request.UserLanguages[0]);
			}


			//linkReply.NavigateUrl=string.Format("forumPostReply.aspx?PostID={0}&CategoryID={1}&ForumName={2}&CategoryName={3}", 
			//	PostID, CategoryID,ForumName,CategoryName);
			//linkReply1.NavigateUrl=linkReply.NavigateUrl;
		}

		public string GetPostAndReplyDisplay()
		{
			string PostID = Request.QueryString["PostID"];
			string CategoryID = Request.QueryString["CategoryID"];
			string ForumID = Request.QueryString["ForumID"];
			string currentPage = Request.QueryString["CurrPage"];
			if (currentPage == null || currentPage == "") 
			{
				currentPage = "0";
			}
			ForumDataAccess da = new ForumDataAccess();

			int TotalTopics = 0;
			int TopicsPerPage = 0;

			bool foundPost = false;

			int i = 0;
			DataTable bbsTable = null;
			StringBuilder sb = new StringBuilder();

			do 
			{
				DataSet bbsDS = 
					da.GetForumCategoryPosts(ForumID,CategoryID,currentPage,null,
					out TotalTopics, out TopicsPerPage);

			
				
				bbsTable = bbsDS.Tables[0];
			
				i = 0;
				while (i < bbsTable.Rows.Count &&
					Convert.ToString(bbsTable.Rows[i]["PostID"]) != PostID) 
				{
					i++;				
				}

				if (i < bbsTable.Rows.Count) 
					foundPost = true;
				else 
				{
                    foundPost = false;
					currentPage = Convert.ToString(Convert.ToInt32(currentPage) + 1);
				}

			} while (!foundPost);

			// we found the current PostID, display it. 
			//sb.Append("<font color=blue size=2>-");
			//sb.Append(bbsTable.Rows[i]["Subject"] + "</font><br>");
			int currLevel = Convert.ToUInt16(bbsTable.Rows[i]["Level"]);
            
			i++;
			// To find all the replies
			while (i < bbsTable.Rows.Count &&
				Convert.ToUInt16(bbsTable.Rows[i]["Level"]) > currLevel )
			{
				int level = Convert.ToUInt16(bbsTable.Rows[i]["Level"]);
				for (int j=1;j<level;j++)
					sb.Append("&nbsp;&nbsp;");
				sb.Append("<font size =3> <b>.</b> </font>");

				sb.Append(string.Format("<a href=forumPostDetail.aspx?PostID={0}&CategoryID={1}&ForumID={2} style=text-decoration:none>", 
					Convert.ToString(bbsTable.Rows[i]["PostID"]), 
					Convert.ToString(bbsTable.Rows[i]["CategoryID"]),
					ForumID));

				sb.Append("<font color=green>");
				
				sb.Append(Convert.ToString(bbsTable.Rows[i]["Subject"]));	// subject
				sb.Append("</font>");
				//if (size!=0)
				sb.Append("</a>");

				if (level ==1) 
				{
					sb.Append("-<font color=blue size=2>");
				} 
				else 
				{
					sb.Append("-<font color=green size=2>");
				}

				sb.Append(Convert.ToString(bbsTable.Rows[i]["CreateUser"]));
				DateTime dt = System.Convert.ToDateTime(Convert.ToString(bbsTable.Rows[i]["CreateDate"]));
				sb.Append("-" + dt);
				
				sb.Append("</font></br>");
				//sb.Append(bbsTable.Rows[i]["Subject"] + "<br>");
				i++;
			};

			
			return sb.ToString();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private void LinkButton1_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("forumsStart.aspx");
		}

		private void TextBox1_TextChanged(object sender, System.EventArgs e)
		{
		
		}

		private void TEXTAREA1_ServerChange(object sender, System.EventArgs e)
		{
		
		}

		protected void Button1_Click(object sender, System.EventArgs e)
		{
		  string PostID = Request.QueryString["PostID"];
		  string CategoryID = Request.QueryString["CategoryID"];
		  string ForumName = Request.QueryString["ForumName"];
		  string CategoryName = Request.QueryString["CategoryName"];
		  Response.Redirect(string.Format("forumPostReply.aspx?PostID={0}&CategoryID={1}&ForumName={2}&CategoryName={3}", 
				PostID, CategoryID,ForumName,CategoryName));
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
		   Button1_Click(sender, e);
		}

        protected void Button1_Click1(object sender, EventArgs e)
        {
            string PostID = Request.QueryString["PostID"];
            string CategoryID = Request.QueryString["CategoryID"];
            string ForumName = Request.QueryString["ForumName"];
            string CategoryName = Request.QueryString["CategoryName"];
            Response.Redirect(string.Format("forumPostReply.aspx?PostID={0}&CategoryID={1}&ForumName={2}&CategoryName={3}",
                  PostID, CategoryID, ForumName, CategoryName));
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            Button1_Click1(sender, e);
        }
	}
}
